import subjectApi from '@/api/subject'
import examPaperApi from '@/api/examPaper'
import questionApi from '@/api/question'

const state = {
  subjects: [],
  paperNameList: [],
  questionTitleList: []
}

const getters = {
  subjectEnumFormat: (state) => (key) => {
    for (let item of state.subjects) {
      if (item.id === key) {
        return item.name + ' ( ' + item.levelName + ' )'
      }
    }
    return null
  }
}

// actions
const actions = {
  initSubject({ commit }, action) {
    subjectApi.list().then(re => {
      commit('setSubjects', re.response)
      if (action !== undefined) {
        action()
      }
    })
  },
  initPaperNameList({ commit }, action) {
    examPaperApi.paperNameList().then(re => {
      let x  = [];
      re.response.map((item) => {
        x.push({
          value: item
        })
      })
      commit('setPaperNameList', x)
      if (action !== undefined) {
        action()
      }
    })
  },
  initQuestionTitleList({ commit }, action) {
    questionApi.questionTitleList().then(re => {
      let x  = [];
      re.response.map((item) => {
        x.push({
          value: item
        })
      })
      commit('setQuestionTitleList', x)
      if (action !== undefined) {
        action()
      }
    })
  }
}

// mutations
const mutations = {
  setSubjects: (state, subjects) => {
    state.subjects = subjects
  },
  setPaperNameList: (state, paperNameList) => {
    state.paperNameList = paperNameList;
  },
  setQuestionTitleList: (state, questionTitleList) => {
    state.questionTitleList = questionTitleList;
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
